/*
* Copyright (c) 2010-2013 Ruben Chakhmakhchyan. All rights reserved.
* 
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Eclipse Public License
* Version 1.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://opensource.org/licenses/EPL-1.0 and read it before using this file.
*/

#pragma once

#include <Windows.h>
#include "commontypes.h"


bool varIsNumeric( VARIANT* pv );

bool varIsString( VARIANT* pv );

template<typename T>
bool varAsNumeric( VARIANT* pv, T& value )
{
    VARIANT& v = *pv;

    switch( v.vt )
    {
    case VT_I1: value = static_cast<T>( v.cVal ); break;
    case VT_I2: value = static_cast<T>( v.iVal ); break;
    case VT_I4:
    case VT_INT: value = static_cast<T>( v.intVal ); break;
    case VT_I8: value = static_cast<T>( v.llVal ); break;

    case VT_UI1: value = static_cast<T>( v.bVal ); break;
    case VT_UI2: value = static_cast<T>( v.uiVal ); break;
    case VT_UI4:
    case VT_UINT: value = static_cast<T>( v.uintVal ); break;
    case VT_UI8: value = static_cast<T>( v.ullVal ); break;

    case VT_R4: value = static_cast<T>( v.fltVal ); break;
    case VT_R8: value = static_cast<T>( v.dblVal ); break;

    default:
        return false;
    }
    return true;
}

template<typename T>
T varAsNumeric( VARIANT* pv )
{
    VARIANT& v = *pv;
    T value = 0;

    switch( v.vt )
    {
    case VT_I1: value = static_cast<T>( v.cVal ); break;
    case VT_I2: value = static_cast<T>( v.iVal ); break;
    case VT_I4:
    case VT_INT: value = static_cast<T>( v.intVal ); break;
    case VT_I8: value = static_cast<T>( v.llVal ); break;

    case VT_UI1: value = static_cast<T>( v.bVal ); break;
    case VT_UI2: value = static_cast<T>( v.uiVal ); break;
    case VT_UI4:
    case VT_UINT: value = static_cast<T>( v.uintVal ); break;
    case VT_UI8: value = static_cast<T>( v.ullVal ); break;

    case VT_R4: value = static_cast<T>( v.fltVal ); break;
    case VT_R8: value = static_cast<T>( v.dblVal ); break;

    default:
        return 0;
    }
    return value;
}